<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/12
     * Time: 下午8:54
     */

    namespace app\store\admin;

    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\member\model\MemberModel;
    use app\payment\logic\PayBase;
    use app\store\logic\DeliverGoodsLogic;
    use app\store\model\LogisticsModel;
    use app\store\model\OrderInfoModel;
    use app\store\model\OrderModel;

    class Logistics extends Admin
    {

        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function lists()
        {

            // 查询
            $map = $this->getMap();
            // 排序
            $order = $this->getOrder('id desc');

            $status    = [
                OrderModel::STATUS_YES_PAY,
                OrderModel::STATUS_DELIVER,
                OrderModel::STATUS_DELIVER_SIGN,
                OrderModel::STATUS_DELIVER_REFUND,
                OrderModel::STATUS_DELIVER_REFUND_YES,
                OrderModel::STATUS_DELIVER_REFUND_NO,
            ];
            $data_list = OrderModel::where($map)->where('status < 90')->whereIn('status',$status)->order($order)->paginate();

            $RightButtons = [
                'send' => [
                    'title' => '发货',
                    'icon'  => 'fa fa-fw fa-navicon',
                    'href'  => url('send',['order_sn' => '__order_sn__'])
                ]
            ];

            return ZBuilder::make('table')->setPageTitle('商品管理')->setSearch(['order_sn' => '订单编号'])// 设置搜索框
            ->addColumns([ // 批量添加数据列
                           ['id','ID'],
                           ['order_sn','订单编号','text'],
                           ['pay_sn','支付订单编号','text'],
                           ['pay_type','支付方式','text','',PayBase::$PAY_TYPE],
                           ['buyer_id','买方','text','',MemberModel::GetIDsUrl($data_list,'buyer_id')],
                           [
                               'address',
                               '地区',
                               'callback',
                               function ($value)
                               {

                                   return json_decode($value,true)['addr'];
                               }
                           ],
                           ['amount','订单总价','text'],
                           ['status','订单状态','text','',OrderModel::STATUS_VIEW],
                           ['right_button','操作','btn']
            ])->addRightButtons($RightButtons)// 批量添加右侧按钮
            ->addOrder('goods_id')->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染模板
        }

        public function send($order_sn)
        {


            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据
                $data = $this->request->post();

                $order = OrderModel::where('order_sn',$order_sn)->find();
                if ($order)
                {
                    DeliverGoodsLogic::Send($order_sn,$data['code'],$data['company']) ? $this->success('操作成功！') : $this->error('操作失败');

                }
                $this->error('订单不存在');

            }

            $order_sn ? $order_sn : $this->error('参数错误');
            $order = OrderModel::where('order_sn',$order_sn)->find();

            $type              = $order['status'] == OrderModel::STATUS_YES_PAY;
            $order['pay_type'] = PayBase::$PAY_TYPE[$order['pay_type']];

            $order['status'] = OrderModel::STATUS_VIEW[$order['status']];
            $order_info      = OrderInfoModel::where('order_sn',$order_sn)->select();
            foreach ($order_info as &$item)
            {
                $item['goods_image'] = get_file_path($item['goods_image']);
            }
            unset($item);

            $express = LogisticsModel::where('order_sn',$order_sn)->find();
            if ($express)
            {
                $this->assign('express',$express);
            }

            $this->assign('type',$type);
            $this->assign('order',$order);
            $this->assign('order_info',$order_info);
            $address = json_decode($order['address'],true);
            $this->assign('address',$address);
            return $this->fetch('order/send');
        }
    }